(function($) {
	var groupCode = '',
		groupDesc = '',
		locId = '',
		locDesc = '';
	var nurArr = [];
	$.init({
		swipeBack: true //启用右滑关闭功能
	}); 
	$.plusReady(function() {
		var currentLoc = getLocalData('currentLoc');
		if(currentLoc) {
			groupCode = currentLoc['GroupCode'];
			groupDesc = currentLoc['GroupDesc'];
			locId = currentLoc['LocId'];
			locDesc = currentLoc['LocDesc'];
		} else {
			mui.toast('用户未登录');
			mui.openWindow('logon-shell.html', 'logon-shell');
			return;
		}
		mui('.mui-scroll-wrapper').scroll();
		var applyNur = document.getElementById('applyNur');
		var pinfo = getLocalData('pinfo');
		applyNur.innerText = pinfo.PersonName;
		//调换日期
		var liApplyDate = document.getElementById('li-applyDate');
		
		//换班对象
		var lichangeNur = document.getElementById('li-changeNur');
		lichangeNur.addEventListener('tap', lichangeNurClicked, false);
		//调换日期
		var lichangeDate = document.getElementById('li-changeDate');
		
		if (plus.os.name == 'iOS') {
			liApplyDate.addEventListener('tap', iosDateShow, false);
			lichangeDate.addEventListener('tap', iosDateShow, false);
		}else{
			liApplyDate.addEventListener('tap', lichangeDateClicked, false);
			lichangeDate.addEventListener('tap', lichangeDateClicked, false);
		}
		//提交
		var submitBtn = document.getElementById('submitBtn');
		submitBtn.addEventListener('tap', submitBtnClicked, false);
		getPersonList();
	});
	jQuery("#textarea").on("input propertychange", function() {
		var $this = jQuery(this),
			_val = $this.val(),
			count = "";
		if(_val.length > 100) {
			$this.val(_val.substring(0, 100));
			mui.toast('最多输入100个字符')
		}
		count = $this.val().length;
		jQuery("#text-count").text(count);
	});

	function getPersonList(show) {
		if(show) {
			plus.nativeUI.showWaiting("获取换班对象",{modal:false});			
		}
		//获取科室排班
		var changeDate = document.getElementById('changeDate');
		var dateStr = changeDate.innerText;
		var xmlStr = '<Request>' + locId + '</Request>';
		var methodStr = 'nurse.schedule.nurgetlocnurlist';
		postHttpRequest(xmlStr, methodStr, function(result) {
			plus.nativeUI.closeWaiting();
			if(result) {
				var resultDict = eval('(' + result + ')');
				nurArr = resultDict['info'];
				updateNurPopoverUI();
				if (show) {
					mui('#nurPopo').popover('toggle');
				}
			} else {
				mui.toast("未能获取到数据（result为空）");
			}
		}, function(errorStr) {
			mui.toast(errorStr);
			plus.nativeUI.closeWaiting();
		});
	}

	/*
	 * 换班日期
	 */
	function lichangeDateClicked() {
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		var _self = this;
		if(_self.picker) {
			_self.picker.show(function(rs) {
				var dateStr = rs.text;
				var changeDate = _self.querySelector('.mui-badge');
				changeDate.innerText = dateStr;
				_self.picker.dispose();
				_self.picker = null;
				getPersonSchInfo(dateStr, changeDate.id);
			});
		} else {
			var optionsJson = '{"type":"date"}';
			var options = JSON.parse(optionsJson);
			_self.picker = new mui.DtPicker(options);
			_self.picker.show(function(rs) {
				var dateStr = rs.text;
				var changeDate = _self.querySelector('.mui-badge');
				changeDate.innerText = dateStr;
				getPersonSchInfo(dateStr, changeDate.id);
			});
		}
	}
	function iosDateShow(){
		var btnDom = this
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			var changeDate = btnDom.querySelector('.mui-badge');
			changeDate.innerText = dateStr;
			getPersonSchInfo(dateStr, changeDate.id);
		}, function(e) {}, {});
	}
	/*
	 * 换班对象
	 */
	function lichangeNurClicked() {
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		if(nurArr.length == 0) {
			getPersonList(true);
		} else {
			mui('#nurPopo').popover('toggle');
		}
	}
	 
	function updateNurPopoverUI(){
		var currentWidth = screen.width;
		var nurCount = nurArr.length; 
		var oneLineCount = 6;
		var cellWidth = (currentWidth - (oneLineCount+1)*10) / oneLineCount;
		var lineCount = parseInt(nurCount / oneLineCount);
		var yuCount = nurCount % oneLineCount;
		if (yuCount != 0) {
			lineCount += 1;
		}
		var areaHtml = '';
		for (var i = 0; i < lineCount;i++) {
			var count = oneLineCount;
			var yuTd = '';
			if (yuCount != 0 && i == lineCount-1) {
				count = yuCount;
				yuTd = '<td colspan="'+(oneLineCount - count)+'"></td>'
			}
			var trHtml = '<tr>';
			for (var j = 0; j < count;j++) {
				var index = i*oneLineCount + j;
				var strArr = nurArr[index].split('^');
				var html = '<td id="nur-'+index+'" class="td-code" data-pid="'+strArr[0]+'">'+
					'<div class="centerP"><div class="centerBody">'+strArr[1]+
					'</div></div></td>';
				trHtml += html;
			}
			trHtml += yuTd + '</tr>';
			areaHtml += trHtml;
		}
		var nurArea = document.getElementById('nurArea');
		nurArea.innerHTML = areaHtml;
	
		//添加点击方法 
		var tds = nurArea.querySelectorAll('.td-code');
		for (var i = 0;i < tds.length;i++) {
			 tds[i].addEventListener('tap',nurListBtnTapped);
		}
	}
	
	function nurListBtnTapped(){
		event.stopPropagation()
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		var nurArea = document.getElementById('nurArea');
		var oldActive = nurArea.querySelector('.dhc-active');
		if (oldActive && oldActive.id != this.id) {
			oldActive.classList.remove('dhc-active');
		}
		this.classList.add('dhc-active');
		var nameE = this.querySelector('.centerBody');
		var pid = this.getAttribute('data-pid');
		var changeNur = document.getElementById('changeNur');
		changeNur.innerText = nameE.innerText;
		changeNur.setAttribute('data-pid',pid);
		mui('#nurPopo').popover('toggle');
		var changeDate = document.getElementById('changeDate');
		if(changeDate.innerHTML != '请选择') {
			getPersonSchInfo(changeDate.innerText, 'changeDate');
		}
	}

	/*
	 * 提交申请
	 */
	function submitBtnClicked() {
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		var applyDate = document.getElementById('applyDate');
		if(applyDate.innerText == '请选择') {
			mui.toast('请选择申请换班日期');
			return;
		}
		var changeNur = document.getElementById('changeNur');
		if(changeNur.innerText == '请选择') {
			mui.toast('请选择换班对象');
			return;
		}
		var changeDate = document.getElementById('changeDate');
		if(changeDate.innerText == '请选择') {
			mui.toast('请选择换班日期');
			return;
		}
		var textareaE = document.getElementById('textarea');
		var applyReason = textareaE.value;
		if(applyReason == '') {
			mui.toast('请填写申请理由');
			return
		}
		plus.nativeUI.showWaiting("正在提交",{modal:false}); //80150
		var PerId = getLocalData('pinfo').rw;
		var changePid = changeNur.getAttribute('data-pid');
		var xmlStr = '<Request>^'+locId+'^'+PerId+'^'+applyDate.innerText+'^'+
			changePid+'^'+changeDate.innerText+'^'+applyReason+'</Request>';
		// console.log('xmlStr',xmlStr);
		var methodStr = 'nurse.schedule.nurshiftsave';
		postHttpRequest(xmlStr, methodStr, function(result) {
			plus.nativeUI.closeWaiting();
			var resultDict = eval('(' + result + ')');
			if(resultDict['success'] == true) {
				plus.storage.setItem('detail','1');
				mui.toast('提交成功');
				mui.back();
			}else {
				mui.toast('提交失败,请重试');
			}
		}, function(errorStr) {
			mui.toast(errorStr);
			plus.nativeUI.closeWaiting();
		});
	}
	/*
 	* 根据日期请求当天班次
 	*/
	function getPersonSchInfo(dateStr, idStr) {
		var PerId = getLocalData('pinfo').rw;
		if(idStr == 'changeDate') {
			var changeNur = document.getElementById('changeNur');
			PerId = changeNur.getAttribute('data-pid');
			if(PerId == null || PerId == '') {
				return;
			}
		}
		plus.nativeUI.showWaiting("获取排班内容",{modal:false});//80151
		var xmlStr = '<Request>' + PerId + '^' + dateStr + '</Request>';
		var methodStr = 'nurse.schedule.getnurargjob';
		postHttpRequest(xmlStr, methodStr, function(result) {
			plus.nativeUI.closeWaiting();
			var dict = eval('(' + result + ')');
			var schStr = dict['info'];
			if(schStr == null || schStr == '') {
				mui.toast('所选日期未发布排班');
				return;
			}
			var schId = 'applySch';
			if(idStr == 'changeDate') {
				schId = 'changeSch';
			}
			var schE = document.getElementById(schId);
			schE.innerHTML = schStr;
		}, function(errorStr) {
			mui.toast(errorStr);
			plus.nativeUI.closeWaiting();
		});
	}
})(mui);
/*
 * rw^DepId(请假时护士所在病区)^PerId(申请护士人员Id)^ApplicationDate(申请换班日期)^ShiftPer(被换班人Id)^ShiftDate(被换班日期)
 */